<!--
 * @Description:
 * @Author: zhaolei
 * @Date: 2023-04-13 12:24:10
-->
<template>
  <el-dialog
    title="被举报内容详情"
    :visible.sync="visible"
    width="60%"
    append-to-body
  >
    <avue-form
      ref="addForm"
      v-model="form"
      :option="tableOption"
    ></avue-form>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex';
import { tableOption } from '@/const/crud/content/report-diary';
// import { BROADCAST_TYPE } from '@/const/type';
import { getObj } from '@/api/core/reportManage';
import { remote } from '@/api/admin/dict';
import { REPORTING_CONTENT_TYPE } from '@/const/type';
export default {
  name: 'reportDiaryPopup',
  props: {
    id: {
      type: [Object, String],
    },
    value: {
      type: Boolean,
      default: false,
    },
    isComment: {
      type: Boolean,
      default: false,
    },
    action: {
      type: String,
    },
  },
  data() {
    return {
      form: {},
      loading: false,
      diaryType: [],
      diaryTypeOther: [],
      REPORTING_CONTENT_TYPE,
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
    ...mapGetters(['userInfo', 'access_token']),
    tableOption() {
      return tableOption(
        this.typeList,
        this.isComment,
        this.action === REPORTING_CONTENT_TYPE.DYNAMIC
      );
    },
    typeList() {
      return [
        ...this.diaryType,
        ...this.diaryTypeOther,
        ...[
          {
            label: '随手记', // 随手记
            value: 'note_taking',
          },
        ],
      ];
    },
  },
  watch: {
    visible(val) {
      if (val) {
        this.$nextTick(async () => {
          this.id && this.getDetail();
          await this.getDiaryType();
          await this.getDiaryTypeOther();
        });
      } else {
        this.$refs.addForm && this.$refs.addForm.resetForm();
      }
    },
  },
  methods: {
    async getDiaryType() {
      const { data } = await remote('diary_type');
      this.diaryType = data.data;
    },
    async getDiaryTypeOther() {
      const { data } = await remote('diary_type_other');
      this.diaryTypeOther = data.data;
    },
    async getDetail() {
      const res = await getObj(this.id);
      const data = res.data.data;
      if (this.action !== REPORTING_CONTENT_TYPE.DYNAMIC) {
        if (data.petDiaryBO) {
          this.form = { ...data.petDiaryBO };
          this.form.objType = data.objType;
          this.form.commentContent = data.commentContent;
          if (Array.isArray(JSON.parse(this.form.files))) {
            const temp = JSON.parse(this.form.files);
            console.log('temp', temp);
            this.form.petPicture = temp.map((item) => {
              let type = 'mov';
              if (item.type === 'image') {
                type = 'jpg';
              }
              return `${item.url}?.${type}`;
            });
          }
        }
      } else {
        if (data.shareResDTO) {
          this.form = { ...data.shareResDTO };
          this.form.objType = data.objType;
          this.form.commentContent = data.commentContent;
          if (Array.isArray(JSON.parse(this.form.files))) {
            const temp = JSON.parse(this.form.files);
            console.log('temp', temp);
            this.form.petPicture = temp.map((item) => {
              let type = 'mov';
              if (item.type === 'image') {
                type = 'jpg';
              }
              return `${item.url}?.${type}`;
            });
          }
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
